package scoreAnalyze;
import java.io.IOException;
import org.apache.hadoop.io.FloatWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

/**
 * 过滤文件头部不合法数据
 * 直接获取学生的姓名和学号
 * 并计算出学生的平均成绩
 */
public class StudentMapper
            extends Mapper<LongWritable, Text, StudentWritable, FloatWritable>
{
    public void map(LongWritable k1, Text v1, Context context)
        throws IOException, InterruptedException{
        String line = v1.toString();
        if(line.contains("id")){
            return;
        }
        String[] splits = line.split(" ");
        if(splits.length != 4)
            return;
        StudentWritable s = new StudentWritable(splits[0], splits[1]);
        int sum = Integer.parseInt(splits[2]) + Integer.parseInt(splits[3]);
        float average = sum / 2;
        context.write(s, new FloatWritable(average));
    }
}
